﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using  Fcdbas.CodeBuilder.TemplateCode;

namespace Fcdbas.CodeBuilder
{
    public partial class NewModPart : Form
    {
        public NewModPart()
        {
            InitializeComponent();
        }
        public NewModPart(LogicUnit lu,string fileType)
        {
            this._lu = lu;
            this._fileType = fileType;
            InitializeComponent();
        }

        public NewModPart(LogicUnit lu, string templateFile, string fileType)
        {
            this._lu = lu;
            this._fileType = fileType;
            InitializeComponent();
            this.txtCDBFile.Text = templateFile;
        }

        private LogicUnit _lu;
        private string  _fileType;

        private void NewModPart_Load(object sender, EventArgs e)
        {
            this.textBox1.Text = _lu.ModelName;
            this.txtPAKFile.Text = _fileType;
            this.txtPAKFile.Enabled = false;
            this.button2.Enabled = false;
            switch (_fileType)
            {
                case "CDB":
                    this.txtPAKFile.Text = _lu.CDBFile;
                    this.txtCDBFile.Text = (this.Owner as Fcdbas.Tool.CodeBuilder).CurProject.DefaultCDBFile;
                    this.txtPAKFile.Text = (this.Owner as Fcdbas.Tool.CodeBuilder).CurProject.DefaultCDBFile;
                    break;
                case "PKG":
                    this.txtPAKFile.Text = _lu.PKGFile;
                    this.txtCDBFile.Text = (this.Owner as Fcdbas.Tool.CodeBuilder).CurProject.DefaultPKGFile;
                    this.txtPAKFile.Text = (this.Owner as Fcdbas.Tool.CodeBuilder).CurProject.DefaultPKGFile;
                    break;
                case "RPT":
                    break;
                case "WEB":
                    break;
                case "BBL":
                    break;
                default:
                    break;
            }
            //this.txtCDBFile.Text = (Owner as Fcdbas.Tool.CodeBuilder)
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            switch (_fileType)
            {
                case "CDB":
                    _lu.CDBFile = chkDefault.Checked ? this.txtPAKFile.Text : this.txtCDBFile.Text;
                    break;
                case "PKG":
                    _lu.PKGFile = chkDefault.Checked ? this.txtPAKFile.Text : this.txtCDBFile.Text;
                    break;
                case "RPT":
                    break;
                case "WEB":
                    break;
                case "BBL":
                    break;
                default:
                    break;
            }
            if (chkDefault.Checked && string.IsNullOrEmpty(this.txtPAKFile.Text))
            {
                MessageBox.Show("Please select your template file and try!");
                return;
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            switch (_fileType)
            {
                case "CDB":
                    this.opTemplateFile.Filter = "模版文件(*.cdb)|*.cdb";
                    break;
                case "PKG":
                    this.opTemplateFile.Filter = "模版文件(*.pkg)|*.pkg";
                    break;
                case "RPT":
                    this.opTemplateFile.Filter = "模版文件(*.rpt)|*.rpt";
                    break;
                case "WEB":
                    this.opTemplateFile.Filter = "模版文件(*.web)|*.web";
                    break;
                case "BBL":
                    break;
                default:
                    break;
            }
            //this.opTemplateFile.Filter = "模版文件(*.pkg)|*.pkg";
            if (DialogResult.OK == this.opTemplateFile.ShowDialog())
            {
                string file = this.opTemplateFile.FileName;
                this.txtPAKFile.Text = file;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void chkDefault_CheckedChanged(object sender, EventArgs e)
        {
            if (chkDefault.Checked)
            {
                this.txtPAKFile.Enabled = false;
                this.button2.Enabled = false;
            }
            else
            {
                this.txtPAKFile.Enabled = true;
                this.button2.Enabled = true;
            }
        }
   
       
           
    }
}
